package main;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cars.Car;

import players.Player;

import racing.Track;
import utils.Polygonal;
import utils.Position;

public class Play extends JFrame{
	private static final long serialVersionUID = 1L;
	public Track tracing;
	public boolean playing;
	public boolean preparado;
	public Vector<Player> players;
	public int turn;
	public JButton next;
	
	public Play(){
	   super( "Racing-Poli" );
	   
	   Container contenedor = getContentPane();
	   contenedor.setLayout( new FlowLayout() );	     
	   next = new JButton("Next");
	   ManejadorBoton manejador = new ManejadorBoton();
	   next.addActionListener( manejador );
	   contenedor.add(next);
	   
	   playing = false;
	   preparado = false;
	   players = new Vector<Player>();
	   turn = 1;
	   
	   setSize( 400, 300 );
	   setVisible( true );
	}
	
	public void readTrack() throws IOException{
		
		// File file = new File ("/home/ceamha/workspace/racing-poli/src/resources/file1.txt");
		 File file = new File ("D:\\file2.txt");
         FileReader fr = new FileReader (file);
         BufferedReader br = new BufferedReader(fr);

         int n = Integer.valueOf(br.readLine());
         
         Vector<Position> pointsL = new Vector<Position>();
         
         for(int i=1; i<=n; i++){
        	 String position = br.readLine();
        	 String[] points = position.split(" ");
        	 Position p = new Position(Integer.valueOf(points[0]), Integer.valueOf(points[1]));
        	 pointsL.add(p);
         }
         int m = Integer.valueOf(br.readLine());
         Vector<Position> pointsR = new Vector<Position>();
         
         for(int i=1; i<=m; i++){
        	 String position = br.readLine();
        	 String[] points = position.split(" ");
        	 Position p = new Position(Integer.valueOf(points[0]), Integer.valueOf(points[1]));
        	 pointsR.add(p);
         }	
         br.close();
		tracing = new Track(new Polygonal(pointsL), new Polygonal(pointsR));
		selectPlayers();
		
	}
	
	private void selectPlayers() {
		int numberPlayers = Integer.valueOf(JOptionPane.showInputDialog("Numero de Jugadores: "));
		
		for(int n = 1; n<=numberPlayers; n++){
			String name = JOptionPane.showInputDialog("Nombre del Jugador "+(n)+": ");
			int red = Math.abs((int)System.nanoTime()%255);
			int green = Math.abs((int)System.nanoTime()%255);
			int blue = Math.abs((int)System.nanoTime()%255);
			Color color = new Color(red, green, blue);
			Position initial = new Position();
			Position exit = new Position();
			//Si es vertical
			if(tracing.isExitVertical()){
				initial = new Position(tracing.getExit().getA().getX(),tracing.getExit().getA().getY());
				exit = new Position(tracing.getExit().getA().getX(),tracing.getExit().getA().getY()+n);
			}else{
				initial = new Position(tracing.getExit().getA().getX(),tracing.getExit().getA().getY());
				exit = new Position(tracing.getExit().getA().getX()+n,tracing.getExit().getA().getY());
			}
			
			
			Car car = new Car(exit, initial, initial, color);
			car.getRace().addPosition(initial);
			car.getRace().addPosition(exit);
			Player player = new Player(n, name, car);
			players.add(player);
			System.out.println(player.getName()+". "+"Inicial: "+initial.toString()+" - Salida:"+exit.toString());
		}
		playing = true;
	}

	private void drawTrack(Graphics g) {
		g.setColor(Color.LIGHT_GRAY);	
		g.drawPolyline(tracing.getLeft().getPointsX(), tracing.getLeft().getPointsY(), tracing.getLeft().getPointsY().length);		
		g.drawPolyline(tracing.getRight().getPointsX(), tracing.getRight().getPointsY(), tracing.getRight().getPointsY().length);		
		g.setColor(Color.BLUE);
		g.drawLine((int)tracing.getExit().getA().getX(), (int)tracing.getExit().getA().getY(), (int)tracing.getExit().getB().getX(), (int)tracing.getExit().getB().getY());
		g.setColor(Color.RED);
		g.drawLine((int)tracing.getGoal().getA().getX(), (int)tracing.getGoal().getA().getY(), (int)tracing.getGoal().getB().getX(), (int)tracing.getGoal().getB().getY());		
	}
	
	private void drawPlayersExit(Graphics g) {
		for(int i=0; i<players.size(); i++){
			g.setColor(players.get(i).getCar().getColor());
			g.drawPolyline(players.get(i).getCar().getRace().getPointsX(), players.get(i).getCar().getRace().getPointsY(), players.get(i).getCar().getRace().getSegments().size());
//			g.drawLine((int)players.get(i).getCar().getPrevius_position().getX(), (int)players.get(i).getCar().getPrevius_position().getY(),
//					(int)players.get(i).getCar().getCurrent_position().getX(), (int)players.get(i).getCar().getCurrent_position().getY());			
		}
	}
	int time=0;
	public void paint( Graphics g ){
		super.paint( g );
		drawTrack(g);
		drawPlayersExit(g);
		drawPlayers(g);
		
	} 
		
	private void drawPlayers(Graphics g) {
		
		if(playing){
			if(preparado){
				System.out.println(preparado);
				for(int i=0; i<players.size(); i++){
					if(players.get(i).isPlaying()){				
						int option = Integer.valueOf(JOptionPane.showInputDialog(players.get(i).getName()+" siguiente paso?: Ai1, Ac2, Ad3, I4, C5, D6, Bi7, Bc8, Bd9"));
						players.get(i).move(option);
						preparado = false;
						g.setColor(players.get(i).getCar().getColor());
						g.drawPolyline(players.get(i).getCar().getRace().getPointsX(), players.get(i).getCar().getRace().getPointsY(), players.get(i).getCar().getRace().getSegments().size());
					}
				}
				drawPlayers(g);
			}
		}
	}
	
	
	private class ManejadorBoton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			JOptionPane.showMessageDialog( next, "A Jugar! " );
			preparado = true;
		}
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		JFrame.setDefaultLookAndFeelDecorated(true);
		Play aplicacion = new Play();
		aplicacion.readTrack();
		aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

	}

}
